//
//  ZJShareIssueViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/13.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJShareIssueViewController.h"
#import "ZJSIPotoView.h"
#import "ZJSIRemarkView.h"
#import "ZJSILocationView.h"
#import "ZJSIScopeView.h"
#import "ZJSIValidTimeView.h"
#import "ZLPhotoActionSheet.h"
#import <AFNetworking.h>
#import "MyOTTypeView.h"
#import "LPCostomChooseYearAndMonthAndDayDateView.h"
#import "ZJSClainUpLoadModel.h"
#import "ZJUpAuditInfoModel.h"
#import <JCAlertView.h>
#import "LCustomAlertView.h"
#import "STPhotoBroswer.h"
#import "MyICRemarkView.h"
#import "ZJRBTextFliedView.h"
#import "ZJIssueOrganizationSelectView.h"
#import "ZJNeedCategoryView.h"
#import "LPCostomJudgeIsOrFalseView.h"
#import "ZJNeedCaModel.h"
#import "YBPopupMenu.h"
@interface ZJShareIssueViewController ()<ZJSIPotoViewDelegate,ZJSIRemarkViewDelegate,ZJSIValidTimeViewDelegate,LPCostomChooseYearAndMonthAndDayDelegate,ZJSIScopeViewDelegate,ZJSILocationViewDelegate,LCustomAlertDelegate,ZJIssueOrganizationSelectViewDelegate,YBPopupMenuDelegate,LPCostomJudgeIsOrFalseDelegate>
{
    NSInteger _index;/**< 标记图片数组的长度 */
    NSData *_recordData;/**< 录音文件的数据 */
    NSString *_scopeString;/**< 范围 */
    NSString *_conditionString;/**< 面向范围 */
    NSString *_issueTypeString;/**< 需求分类 */
    NSString *_needCategory;/**< 需求类型 */
    NSString *_latitude;/**< 纬度 */
    NSString *_longitude;/**< 经度 */
    ZJUpAuditInfoModel *_auditModel;/**< 需要提交审核的model */
    ZJSClainUpLoadModel * _upNetModel;/**< 上传信息model */
    BOOL _isStreet;/**< 是街道书记 */
    NSString *_isWXY;/**< 是否是微心愿  0 不是 1是 */
    NSArray *_needCategoryArray;/**< 需求类型的下拉框 */
    ZJNeedCaModel *_selectNeedModel;/** 需求分类选择model */
    YBPopupMenu *_needMenu;/** 需求分类的弹出框 */
    LCustomAlertView *_SHView;/**< 审核弹窗的View */
    LCustomAlertView *_deleImagesView;/**< 删除图片弹窗的View */
    NSInteger _deleImageIndex;/**< 删除图片的下标 */
    
    NSString *_organizationType;/**< 党组织类型 0:街镇党组织(默认)  1：大口党组织 */
    ZJIssueOrganModel *_organModel;/**< 大口党组织--选中的党组织信息 */
}
/** scrollview */
@property (nonatomic, strong) UIScrollView *scrollView;
/** 名字 */
@property (nonatomic, strong) ZJRBTextFliedView *name;
/**  是否是微心愿  */
@property (strong, nonatomic) LPCostomJudgeIsOrFalseView *wish;
/** 需求类别 */
@property (nonatomic, strong) ZJNeedCategoryView *needCategoryView;
/** 需求类型 */
@property (nonatomic, strong) ZJSIScopeView *issueForm;
/** 需求分类 */
@property (nonatomic, strong) ZJSIScopeView *issueType;
/** 需求说明 */
@property (nonatomic, strong) ZJSIRemarkView *remarkView;
/** 我的故事 */
@property (nonatomic, strong) MyICRemarkView *myTale;
/** 照片 */
@property (nonatomic, strong) ZJSIPotoView *potoView;
/** 位置 */
@property (nonatomic, strong) ZJSILocationView *locationView;
/** 公开范围 */
@property (nonatomic, strong) ZJSIScopeView *scopeView;
/** 面向范围 */
@property (nonatomic, strong) ZJSIScopeView *astrict;
/** 有效期 */
@property (nonatomic, strong) ZJSIValidTimeView *validTimeView;

/** 时间选择器 */
@property (nonatomic, strong) LPCostomChooseYearAndMonthAndDayDateView *timeSeleView;
/** 图片数组 */
@property (nonatomic, strong) NSMutableArray *imageArray;
/** 弹窗视图 */
@property (nonatomic, strong) JCAlertView *alerView;
/** 弹窗视图 */
@property (nonatomic, strong) JCAlertView *deleAlerView;
/** 发布页面--大口党组织--组织选择的弹窗视图 */
@property (nonatomic, strong) ZJIssueOrganizationSelectView *organizationSelectView;
@end

@implementation ZJShareIssueViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [self.remarkView.recordTool stopPlaying];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _index = self.imageArray.count;
    _scopeString = @"1";
    _conditionString = @"2";
    _issueTypeString = @"1";
    _organizationType = @"0";
    _needCategory = @"1";
    _isWXY = @"0";
    self.titlelabel.text = @"发布需求";
    
    [self netWorking_needArray];
    [self setupUI];
    [self judgeOrganizationType];
    
}

- (void)setupUI{
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.name];
    [self.scrollView addSubview:self.wish];
    [self.scrollView addSubview:self.needCategoryView];
    [self.scrollView addSubview:self.issueForm];
    [self.scrollView addSubview:self.issueType];
    [self.scrollView addSubview:self.remarkView];
    [self.scrollView addSubview:self.myTale];
    [self.scrollView addSubview:self.potoView];
    [self.scrollView addSubview:self.locationView];
    [self.scrollView addSubview:self.scopeView];
    [self.scrollView addSubview:self.validTimeView];
//    [self.scrollView addSubview:self.astrict];
    
    //提示view
    UILabel *label = [[UILabel alloc] init];
    label.text = @"备注 : 涉及金额问题, 原则上不超过500元";
    label.font = [UIFont titleTextFont];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor textBlackColor];
    [self.scrollView addSubview:label];
    //确认button
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:@"确认" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 4 * [UIScreen zj_screenWScale];
    button.titleLabel.font = [UIFont titleTextFont];
    button.backgroundColor = [UIColor themeColor];
    [button addTarget:self action:@selector(action_affirmButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:button];
    
    self.name.sd_layout
    .topSpaceToView(self.scrollView, 0)
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.name.height);
    
    self.wish.sd_layout
    .topSpaceToView(self.name, 0)
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.wish.height);
    
    self.needCategoryView.sd_layout
    .topSpaceToView(self.wish, 5 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.needCategoryView.height);
    
    self.issueForm.sd_layout
    .topSpaceToView(self.needCategoryView,  5 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.issueForm.height);
    
    self.issueType.sd_layout
    .topSpaceToView(self.issueForm, 0)
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.issueType.height);
    
    self.remarkView.sd_layout
    .topSpaceToView(self.issueType, 0)
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.remarkView.height);
    
    self.myTale.sd_layout
    .topSpaceToView(self.remarkView, 0)
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.myTale.height);
    
    self.potoView.sd_layout
    .topSpaceToView(self.myTale, 0)
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.potoView.height);
    
    self.locationView.sd_layout
    .topSpaceToView(self.potoView, 0)
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.locationView.height);
    
    self.scopeView.sd_layout
    .topSpaceToView(self.locationView, 0)
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.scopeView.height);
    
    self.validTimeView.sd_layout
    .topSpaceToView(self.scopeView, 0)
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.validTimeView.height);
    
//    self.astrict.sd_layout
//    .topSpaceToView(self.validTimeView, 0)
//    .leftSpaceToView(self.scrollView, 0)
//    .rightSpaceToView(self.scrollView, 0)
//    .heightIs(self.astrict.height);
    
    label.sd_layout
    .topSpaceToView(self.self.validTimeView, 20 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs([NSString heightWithTextFont:[UIFont titleTextFont]]);

    button.sd_layout
    .topSpaceToView(label, 20 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.scrollView, 38 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.scrollView, 38 * [UIScreen zj_screenWScale])
    .heightIs(44 * [UIScreen zj_screenHScale]);
    
    [self.scrollView setupAutoContentSizeWithBottomView:button bottomMargin:28 * [UIScreen zj_screenHScale]];
}


#pragma mark - 网络请求（需求类型数组）
- (void)netWorking_needArray{
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:@"ds.action.GetDsCode" Parameter:@{@"CODE_GROUP":@"需求类别"} Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            _needCategoryArray = [NSArray yy_modelArrayWithClass:[ZJNeedCaModel class] json:[object objectForKey:@"children"]];
            if (_needCategoryArray.count > 0) {
                _selectNeedModel = _needCategoryArray.firstObject;
                weakSelf.needCategoryView.organizationSelectView.textField.text = [NSString string:_selectNeedModel.CODE_NAME isNilStringReturnDeftString:@""];
            }
        }
    } Fail:^(id object) {
        
    }];
}

#pragma mark - 判断是 街镇党组织 还是 大口党组织
- (void)judgeOrganizationType{
    
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"FROM_UNITID":[NSString string:ZJUSERINFORMODEL.CURRENT_ROLE_MODEL.DEPT_ID isNilStringReturnDeftString:@""]};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getTree] Parameter:dict Success:^(id object) {
        
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            _organizationType = [object objectForKey:@"ISDOWN"];
            if ([[object objectForKey:@"ISDOWN"] isEqualToString:@"0"]) {//街镇党组织
                //三个： 全区  街镇党组织  本支部
              weakSelf.organizationSelectView.dataArray = [[NSArray yy_modelArrayWithClass:[ZJIssueOrganModel class] json:[object objectForKey:@"DEPT_TREE"]] mutableCopy];
                [weakSelf getUserTypeUpdateScopeView];
            }else{//大口党组织
            
            [weakSelf.scopeView minUpdataUI_dataArray:@[@"本支部",@"组织选择",@"全区"] selectString:nil];
            weakSelf.organizationSelectView.dataArray = [[NSArray yy_modelArrayWithClass:[ZJIssueOrganModel class] json:[object objectForKey:@"DEPT_TREE"]] mutableCopy];
            }
        }
    } Fail:^(id object) {
        
    }];
}


#pragma mark - 判断当前用户身份--来改变公开范围的UI界面（街镇党组织）
- (void)getUserTypeUpdateScopeView{
    __weak typeof(self) weakSelf = self;
    if (self.isSecretary) {//是书记    0支部    1 街道
        [self netWorking_secretary:^(NSString *secretaryType) {
            if ([secretaryType isEqualToString:@"1"]) {//街道的书记、
                [weakSelf.scopeView minUpdataUI_dataArray:@[@"街镇党组织",@"全区"] selectString:nil];
                _scopeString = @"2";
                _isStreet = YES;
            }
        }];
    }
}

- (void)netWorking_secretary:(void(^)(NSString * secretaryType))secretary{
    [NetWorkingManager postWithUrlString:[NetWorkingURL informactsSelectValue] Parameter:@{@"ACCOUNTID":ZJUSERINFORMODEL.ORG_ACCOUNT.ID} Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            secretary([object objectForKey:@"DEPTTYPE_DSCODE_VALUE"]);
        }
    } Fail:^(id object) {
        
    }];

}


#pragma mark - 需求类型选择
- (void)acton_tapGester{
    __block NSMutableArray *array = [NSMutableArray array];
    [_needCategoryArray enumerateObjectsUsingBlock:^(ZJNeedCaModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [array addObject:obj.CODE_NAME];
    }];
    [YBPopupMenu showRelyOnView:self.needCategoryView titles:array icons:nil menuWidth:self.view.width / 2 showCount:0  delegate:self];
}

- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu{
    _selectNeedModel = _needCategoryArray[index];
    self.needCategoryView.organizationSelectView.textField.text = _selectNeedModel.CODE_NAME;
}

#pragma mark - 点击事件(发布)

///发布
- (void)action_affirmButton:(UIButton *)sender{
    if (!_organizationType) {
        
        return;
    }
    
    if (self.name.textField.text.length == 0) {
        [YJProgressHUD showSuccess:@"请输入标题" inview:self.view];
        [self.name.textField becomeFirstResponder];
        return;
    }
    if (self.remarkView.textView.text.length == 0) {
        [YJProgressHUD showSuccess:@"请填写备注" inview:self.view];
        [self.remarkView.textView becomeFirstResponder];
        return;
    }
    

    if (self.myTale.textView.text.length == 0) {
        [YJProgressHUD showSuccess:@"请填写需求者的故事" inview:self.view];
        [self.myTale.textView becomeFirstResponder];
        return;
    }

    if (self.validTimeView.time.text.length == 0) {
        [YJProgressHUD showSuccess:@"请选择有效期" inview:self.view];
        return;
    }
    
    [self networkingDataDict];
   
}
//准备网络请求数据
- (void)networkingDataDict{
    ZJSClainUpLoadModel *model = [ZJSClainUpLoadModel new];

    model.SYS_ACCOUNTID = ZJUSERINFORMODEL.ORG_ACCOUNT.ID ? ZJUSERINFORMODEL.ORG_ACCOUNT.ID :@"";
    model.SYS_PARTYMEMBERID = ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID ? ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID : @"";
    model.UPLOAD_FILES = self.imageArray;
    model.CONTENT = self.remarkView.textView.text;
    model.TORANG = _conditionString;
    model.EXPECT_STATE = _scopeString;
    model.USERNAME = [ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.USER_NAME;
    model.USERPHONE = [ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.MOBILE;
    model.CREATEUSER = [ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID;
    model.ADDRESS = self.locationView.loactionLabel.text;
    model.VALIDITY = self.validTimeView.time.text ? self.validTimeView.time.text: @"暂无";
    model.LAT = _latitude;
    model.LNG = _longitude;
    model.FROM_USERID = [ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID;
    model.TITLE = self.name.textField.text;
    model.STORY = self.myTale.textView.text;
    model.WXY = _isWXY;
    model.XQLB = _selectNeedModel.CODE_VALUE;
    model.XQLBTEXT = _selectNeedModel.CODE_NAME;
    model.REQUIRE_TYPE = _needCategory;
    model.CREATEACCOUNT = [ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.ID;
    if (self.isSecretary) {//是书记工作台进入
        model.FROM = @"2";
        model.FROM_UNITID = [ZJUserInfoModel sharedUserInfo].CURRENT_ROLE_MODEL.DEPT_ID;
    }else{
        model.FROM = @"1";
        model.FROM_UNITID = [ZJUserInfoModel sharedUserInfo].CURRENT_DEPT_MODEL.ID;
    }
    model.REQUIREMENTCLASS = _issueTypeString;
    if (_recordData) {//录音文件存在
        NetWorkUploadFileModel *recordModel = [NetWorkUploadFileModel new];
        recordModel.FILE_PATH = _recordData;
        recordModel.NAME = @"TAPE";
        recordModel.FILE_INDEX = @"100";
        model.frequency = recordModel;
    }
    model.SENDTO_DAPTID = @"";
    if ([_organizationType isEqualToString:@"1"]) {//大口党组织
        if ([_scopeString isEqualToString:@"1"]) {//本支部
            model.SENDTO_DAPTID = [NSString string:ZJUSERINFORMODEL.CURRENT_ROLE_MODEL.DEPT_ID isNilStringReturnDeftString:@""];
        }else if ([_scopeString isEqualToString:@"2"]) {//组织选择
            model.SENDTO_DAPTID = [NSString string:_organModel.ID isNilStringReturnDeftString:@""];
        }else if ([_scopeString isEqualToString:@"3"]) {//全区
            ZJIssueOrganModel *orgmodel = self.organizationSelectView.dataArray[0];
            model.SENDTO_DAPTID = [NSString string:orgmodel.ID isNilStringReturnDeftString:@""];
        }
    }else{//街镇党组织
        //self.organizationSelectView.dataArray三个： 全区  街镇党组织  本支部
        if ([_scopeString isEqualToString:@"1"]) {//本支部
            ZJIssueOrganModel *orgmodel = self.organizationSelectView.dataArray[2];
            model.SENDTO_DAPTID = [NSString string:orgmodel.ID isNilStringReturnDeftString:@""];
        }else if ([_scopeString isEqualToString:@"2"]) {//街道党组织
            ZJIssueOrganModel *orgmodel = self.organizationSelectView.dataArray[1];
            model.SENDTO_DAPTID = [NSString string:orgmodel.ID isNilStringReturnDeftString:@""];
        }else if ([_scopeString isEqualToString:@"3"]) {//全区
            ZJIssueOrganModel *orgmodel = self.organizationSelectView.dataArray[0];
            model.SENDTO_DAPTID = [NSString string:orgmodel.ID isNilStringReturnDeftString:@""];
        }
    }
    _upNetModel = model;
    [self senderNetWorkingModel:model];
}

//网络请求
- (void)senderNetWorkingModel:(NetWorkUpLoadModel *)parameter{
    [YJProgressHUD showMessage:@"发布中..." duration:10 view:self.view];
    __weak typeof(self) weakSelf = self;
    ZJSClainUpLoadModel *parameter1 = (ZJSClainUpLoadModel *)parameter;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/javascript",@"text/plain", nil];
    
    //1、把文件对象数组转化成数组
    NSArray *fileArray = [parameter1.UPLOAD_FILES yy_modelToJSONObject];
    //2、除去data，得到数组
    __block NSMutableArray *nofileDataArray = [NSMutableArray array];//保存新数组
    
    NSDictionary *netWorkUpLoadModel = [parameter1 yy_modelToJSONObject];
    
    [fileArray enumerateObjectsUsingBlock:^(NSMutableDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeObjectForKey:@"fileUrl"];
        [nofileDataArray addObject:obj];
    }];
    if (parameter1.frequency) {
        NSMutableDictionary *dictVMice = [[parameter1.frequency yy_modelToJSONObject] mutableCopy];
        [dictVMice removeObjectForKey:@"FILE_PATH"];
        [nofileDataArray addObject:dictVMice];
    }
    
    
    NSString * dateString = [NSString stringWithFormat:@"FILES_%@_KEYS",[NSString getCurrentDate]];
    
    [manager POST:UPLOADURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        [netWorkUpLoadModel.allKeys enumerateObjectsUsingBlock:^(NSString *  _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
            //不为fileArray 且有值时
            if (![key isEqualToString:@"UPLOAD_FILES"] && [netWorkUpLoadModel objectForKey:key] && ![key isEqualToString:@"frequency"]) {
                [formData appendPartWithFormData:[netWorkUpLoadModel[key] dataUsingEncoding:NSUTF8StringEncoding] name:key];
            }
        }];
        
        [parameter1.UPLOAD_FILES enumerateObjectsUsingBlock:^(NetWorkUploadFileModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.FILE_PATH) {
                NSData  *fileData= obj.FILE_PATH;//二进制数据
                NSString *mimeType= @"image/jpeg";//文件类型
//                NSString *oldFileNameLast=[obj.FILE_NAME lastPathComponent];//文件原来的名字
                //取文件名的后缀
//                NSString *fileNameLast = [oldFileNameLast zj_subStringLastOfString:@"."];
                //拼接最新的文件名
                NSString *fileName = [NSString stringWithFormat:@"%ld%@",(unsigned long)idx,@".jpg"];
                [formData appendPartWithFileData:fileData name: obj.NAME fileName:fileName mimeType:mimeType];
            }
        }];
        
//        if (parameter1.frequency) {//录音文件存在
//            NSString *fileName = @"100.mp3";
//            [formData appendPartWithFileData:parameter1.frequency.FILE_PATH name: @"TAPE" fileName:fileName mimeType:@"application/octet-stream"];
//            
//        }
    
        [formData appendPartWithFormData:[nofileDataArray yy_modelToJSONData] name:dateString];
        [formData appendPartWithFormData:[[NetWorkingURL addRequirement] dataUsingEncoding:NSUTF8StringEncoding] name:@"ACTION_XML"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //进度
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //成功
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:responseObject];
        if ([model.STATE isEqualToString:@"20000"]) {
            [MobClick endEvent:[UMMStatisticsString publishReq]];
            ZJUpAuditInfoModel *auditModel = [ZJUpAuditInfoModel yy_modelWithJSON:responseObject];
            if ([auditModel.ISCHECK isEqualToString:@"1"]) {//需要提交审核
                _auditModel = auditModel;
                
                UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"该信息需经上级党组织审核后发布,您确定要继续吗" message:nil preferredStyle:UIAlertControllerStyleAlert];
                [alertController addAction:[UIAlertAction actionWithTitle:@"继续" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [weakSelf netWorking_audit];
                }]];
                [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                }]];
                [weakSelf presentViewController:alertController animated:YES completion:nil];
            }else{
                if (weakSelf.block) {
                    weakSelf.block();
                }
                [YJProgressHUD showSuccess:@"发布成功" inview:weakSelf.view];

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
            }
        }else{
                [YJProgressHUD showSuccess:@"发布失败" inview:weakSelf.view];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //失败
        [YJProgressHUD showSuccess:@"发布失败" inview:weakSelf.view];
  
    }];
}
//提交审核
- (void)netWorking_audit{
    [YJProgressHUD showMessage:@"审核中..." duration:10 view:self.view];
    NSString *CONTENT = [NSString stringWithFormat:@"%@%@",_upNetModel.TITLE,_upNetModel.CONTENT ? _upNetModel.CONTENT : @""];
    NSString *SENDTO_DAPTID = @"";
    if ([_organizationType isEqualToString:@"1"]) {//大口党组织
        if ([_scopeString isEqualToString:@"1"]) {//本支部
            SENDTO_DAPTID = [NSString string:ZJUSERINFORMODEL.CURRENT_ROLE_MODEL.DEPT_ID isNilStringReturnDeftString:@""];
        }else if ([_scopeString isEqualToString:@"2"]) {//组织选择
           SENDTO_DAPTID = [NSString string:_organModel.ID isNilStringReturnDeftString:@""];
        }else if ([_scopeString isEqualToString:@"3"]) {//全区
            ZJIssueOrganModel *orgmodel = self.organizationSelectView.dataArray[0];
            SENDTO_DAPTID = [NSString string:orgmodel.ID isNilStringReturnDeftString:@""];
        }
    }else{//街镇党组织
        //self.organizationSelectView.dataArray三个： 全区  街镇党组织  本支部
        if ([_scopeString isEqualToString:@"1"]) {//本支部
            ZJIssueOrganModel *orgmodel = self.organizationSelectView.dataArray[2];
            SENDTO_DAPTID = [NSString string:orgmodel.ID isNilStringReturnDeftString:@""];
        }else if ([_scopeString isEqualToString:@"2"]) {//街道党组织
            ZJIssueOrganModel *orgmodel = self.organizationSelectView.dataArray[1];
            SENDTO_DAPTID = [NSString string:orgmodel.ID isNilStringReturnDeftString:@""];
        }else if ([_scopeString isEqualToString:@"3"]) {//全区
            ZJIssueOrganModel *orgmodel = self.organizationSelectView.dataArray[0];
           SENDTO_DAPTID = [NSString string:orgmodel.ID isNilStringReturnDeftString:@""];
        }
    }


    NSDictionary *dict = @{@"REL_ID":_auditModel.REL_ID,
                           @"EXPECT_STATE":_upNetModel.EXPECT_STATE,
                           @"FROM_USERID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,
                           @"FROM_UNITID":[ZJUserInfoModel sharedUserInfo].CURRENT_ROLE_MODEL.DEPT_ID,
                           @"CONTENT":CONTENT,
                           @"UPDATE":@"0",
                           @"ISMOBILE":@"1",
                           @"TITLE":_upNetModel.TITLE,
                           @"SENDTO_DAPTID":SENDTO_DAPTID};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL publishRq] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//审核成功
            if (weakSelf.block) {
                weakSelf.block();
            }
            [YJProgressHUD showSuccess:@"提交成功" inview:weakSelf.view];
            
        }else{
            [YJProgressHUD showSuccess:@"提交失败" inview:weakSelf.view];
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
    } Fail:^(id object) {
        [YJProgressHUD showSuccess:@"提交失败" inview:weakSelf.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
    }];
}



#pragma mark - LCustomAlertDelegate(弹窗提示) 协议方法
- (void)lCustomAlertWithView:(LCustomAlertView *)view index:(NSInteger)index{
    
    if (view == _SHView) {
        [self.alerView dismissWithCompletion:nil];
        if (index == 1) {
            
            [self netWorking_audit];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }

    }
}

#pragma mark - LPCostomJudgeIsOrFalseDelegate(是否是微心愿) 协议方法
- (void)LPCostomJudgeIsOrFalseWithView:(LPCostomJudgeIsOrFalseView *)view Status:(BOOL)seleted{
    if (seleted) {
        _isWXY = @"1";
    }else{
        _isWXY = @"0";
    }
}

#pragma mark - ZJSILocationViewDelegate(定位) 协议方法
- (void)ZJSILocationViewDelegate_latitude:(NSString *)latitude longitude:(NSString *)longitude{
    _latitude = latitude;
    _longitude = longitude;
}

#pragma mark - ZJIssueOrganizationSelectViewDelegate(发布页面--大口党组织--组织选择的弹窗视图) 协议方法
- (void)ZJIssueOrganizationSelectViewDelegate_cellSelected_Model:(ZJIssueOrganModel *)model{
    _organModel = model;
     [self.scopeView minUpdataUI_dataArray:@[@"本支部",model.DEPT_NAME,@"全区"] selectString:@"1"];
}


#pragma mark - ZJSIScopeViewDelegate(面向范围、资源分类、公开范围的选择) 协议方法
- (void)ZJSIScopeViewClick_scopeView:(ZJSIScopeView *)scopeView index:(NSInteger)index{
    NSString *string = [NSString stringWithFormat:@"%ld",index + 1];
    if (scopeView == self.scopeView) {//公开范围
        _scopeString = string;
        if (_isStreet) {//街道书记
            string = [NSString stringWithFormat:@"%ld",index + 2];
            _scopeString = string;
        }
        if (index == 1 && [_organizationType isEqualToString:@"1"]) {//大口党组织--组织选择
            [self addOrganizationselectview];
        }
    }else if (scopeView == self.astrict) {//"面向范围
        _conditionString = string;
    }else if (scopeView == self.issueType) {//资源分类
        if ([string isEqualToString:@"2"]) {
            _issueTypeString = @"0";
        }else{
            _issueTypeString = string;
        }
    }else if (scopeView == self.issueForm){//需求类型
        _needCategory = string;
    }
}

///添加选择视图 （大口党组织--组织选择）
- (void)addOrganizationselectview{
    self.organizationSelectView.alpha = 0;
    [self.view addSubview:self.organizationSelectView];
    [UIView animateWithDuration:0.5 animations:^{
        self.organizationSelectView.alpha = 1;
    }];
}
#pragma mark - ZJSIRemarkViewDelegate（录音成功或者删除录音） 协议方法
- (void)ZJSIRemarkViewDelegate_RemarkSuccess:(NSData *)remarkData{
    _recordData = remarkData;
}

#pragma mark - ZJSIValidTimeViewDelegate(时间选择) 协议方法
- (void)ZJSIValidTimeViewDelegate_showTime{
    [self.timeSeleView show];
}
#pragma mark - LPCostomChooseYearAndMonthAndDayDelegate 协议方法
- (void)ChooseYearAndMonthAndDayWithTitle:(NSString *)string{
    self.validTimeView.time.text = string;
}
#pragma mark - ZJSIPotoViewDelegate(图片加好) 协议方法
- (void)ZJSIPotoViewDelegate_addPotoClick{
    [self.view endEditing:YES];
    _index = self.imageArray.count;
    //图片多选模块
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    //设置最大选择数量
    actionSheet.maxSelectCount = 9;
    //设置预览图最大数目
    actionSheet.maxPreviewCount = 20;
    __weak typeof(self) weakSelf = self;
    [actionSheet showWithSender:self animate:YES lastSelectPhotoModels:nil completion:^(NSArray<UIImage *> * _Nonnull selectPhotos, NSArray<ZLSelectPhotoModel *> * _Nonnull selectPhotoModels) {
        
        [selectPhotos enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSData * data = [ToolClass compressionImageWithImage:obj compressionMaxLimit:COMPRESSIONMAXLIMIT];
            NetWorkUploadFileModel *model = [NetWorkUploadFileModel new];
            model.FILE_NAME = [NSDateFormatter stringTimeWithDate:[NSDate date]];
            model.FILE_INDEX = [NSString stringWithFormat:@"%ld",(unsigned long)idx];
            model.FILE_PATH = data;
            model.NAME = @"IMG_URL";
            [weakSelf.imageArray addObject:model];
        }];
        _index = self.imageArray.count;
        [weakSelf.potoView updateUI_PotoArray:weakSelf.imageArray];
        weakSelf.potoView.sd_layout
        .topSpaceToView(self.myTale, 0)
        .leftSpaceToView(self.scrollView, 0)
        .rightSpaceToView(self.scrollView, 0)
        .heightIs(self.potoView.height);
        
    }];
}

- (void)ZJSIPotoViewDelegate_deletePotoClick_index:(NSInteger)index{
    _deleImageIndex = index;
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"是否确定删除图片" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.imageArray removeObjectAtIndex:_deleImageIndex];
        _index = self.imageArray.count;
        [self.potoView updateUI_PotoArray:self.imageArray];
        self.potoView.sd_layout
        .topSpaceToView(self.myTale, 0)
        .leftSpaceToView(self.scrollView, 0)
        .rightSpaceToView(self.scrollView, 0)
        .heightIs(self.potoView.height);
   
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)ZJSIPotoViewDelegate_potoImageClick_index:(NSInteger)index{
    __block NSMutableArray *imageDataArray = [NSMutableArray array];
    [self.imageArray enumerateObjectsUsingBlock:^(NetWorkUploadFileModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dict = @{@"image":[UIImage imageWithData:obj.FILE_PATH]};
        [imageDataArray addObject:dict];
    }];
    STPhotoBroswer *view = [[STPhotoBroswer alloc]initWithFrame:CGRectMake(0, 0,self.view.width , self.view.height) ImageArray:imageDataArray currentIndex:index];
    [view show];
}


#pragma mark - Setter
#pragma mark - Getters
- (UIScrollView *)scrollView{
    if (_scrollView == nil){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight)];
        
    }
    return _scrollView;
}

- (ZJNeedCategoryView *)needCategoryView{
    if (_needCategoryView == nil){
        _needCategoryView = [[ZJNeedCategoryView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40 * [UIScreen zj_screenHScale]) title:@"需求类别 :"];
        UITapGestureRecognizer *tapGester = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(acton_tapGester)];
        [_needCategoryView addGestureRecognizer:tapGester];
    }
    return _needCategoryView;
}

- (LPCostomJudgeIsOrFalseView *)wish{
    if (_wish == nil) {
        _wish = [[LPCostomJudgeIsOrFalseView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40 * [UIScreen zj_screenHScale]) judge:@"是否是微心愿 :"];
        _wish.delegate = self;
    }
    return _wish;
}

- (ZJSIScopeView *)issueForm{
    if (_issueForm == nil){
        _issueForm = [[ZJSIScopeView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 195  * [UIScreen zj_screenHScale]) title:@"需求类型 :"];
        [_issueForm minUpdataUI_dataArray:@[@"个人需求",@"团队需求",@"公共需求"] selectString:nil];
        _issueForm.delegate = self;
    }
    return _issueForm;
}

- (ZJSIScopeView *)issueType{
    if (_issueType == nil){
        _issueType = [[ZJSIScopeView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 195  * [UIScreen zj_screenHScale]) title:@"需求分类 :"];
        [_issueType minUpdataUI_dataArray:@[@"区域单位及个人需求",@"属地单位及个人需求"] selectString:nil];
        _issueType.delegate = self;
    }
    return _issueType;
}

- (ZJSIRemarkView *)remarkView{
    if (_remarkView == nil){
        _remarkView = [[ZJSIRemarkView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 195  * [UIScreen zj_screenHScale]) title:@"需求说明 :"];
        _remarkView.maxNumber = 200;
        _remarkView.delegate = self;
    }
    return _remarkView;
}

- (MyICRemarkView *)myTale{
    if (_myTale == nil){
        _myTale = [[MyICRemarkView alloc] initWithFrame:CGRectMake(0, 0, self.view.width,195  * [UIScreen zj_screenHScale] ) title:@"需求者的故事 :"];
        _myTale.maxNumber = 200;
    }
    return _myTale;
}

- (ZJSIPotoView *)potoView{
    if (_potoView == nil){
        _potoView = [[ZJSIPotoView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 195  * [UIScreen zj_screenHScale]) title:@"照片 :"];
        [_potoView updateUI_PotoArray:nil];
        _potoView.deletate = self;
    }
    return _potoView;
}
- (ZJSILocationView *)locationView{
    if (_locationView == nil){
        _locationView = [[ZJSILocationView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 90  * [UIScreen zj_screenHScale]) title:@"位置 :"];
        _locationView.maxNumber = 50;
        _locationView.delegate = self;
    }
    return _locationView;
}
- (ZJSIScopeView *)scopeView{
    if (_scopeView == nil){
        _scopeView = [[ZJSIScopeView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 195  * [UIScreen zj_screenHScale]) title:@"公开范围 :"];
        [_scopeView updataUI_dataArray:@[@"本支部",@"街镇党组织",@"全区"] selectString:nil];
        _scopeView.delegate = self;
    }
    return _scopeView;
}
- (ZJSIScopeView *)astrict{
    if (_astrict == nil){
        _astrict = [[ZJSIScopeView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 195  * [UIScreen zj_screenHScale]) title:@"面向范围 :"];
        [_astrict minUpdataUI_dataArray:@[@"面向组织",@"面向个人"] selectString:nil];
        _astrict.delegate = self;
    }
    return _astrict;
}
- (ZJSIValidTimeView *)validTimeView{
    if (_validTimeView == nil){
        _validTimeView = [[ZJSIValidTimeView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 90  * [UIScreen zj_screenHScale]) title:@"有效期 :"];
        _validTimeView.delegate = self;
    }
    return _validTimeView;
}
- (NSMutableArray *)imageArray{
    if (_imageArray == nil){
        _imageArray = [NSMutableArray array];
        
    }
    return _imageArray;
}
- (LPCostomChooseYearAndMonthAndDayDateView *)timeSeleView{
    if (_timeSeleView == nil){
        _timeSeleView = [[LPCostomChooseYearAndMonthAndDayDateView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
        _timeSeleView.delegate = self;
    }
    return _timeSeleView;
}
- (ZJRBTextFliedView *)name{
    if (_name == nil){
        _name = [[ZJRBTextFliedView alloc] initWithFrame:CGRectMake(0, LP_TopHeight + 20 * [UIScreen zj_screenWScale], self.view.width, 90 * [UIScreen zj_screenHScale]) title:@"标题 :"];
        
        _name.maxNumber = 50;
        _name.titleLabel.frame = CGRectMake(12 * [UIScreen zj_screenWScale], _name.titleLabel.y, self.view.width - 24 * [UIScreen zj_screenWScale], _name.titleLabel.height);
        _name.textField.frame = CGRectMake(12 * [UIScreen zj_screenWScale], _name.textField.y, self.view.width - 24 * [UIScreen zj_screenWScale], _name.textField.height);
     
    }
    return _name;
}
- (JCAlertView *)alerView{
    if (_alerView == nil){
        _SHView = [[LCustomAlertView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight]) alertMessage:@"是否提交审核" titlesArray:@[@"取消",@"确认"]];
        _SHView.delegate = self;
        _alerView = [[JCAlertView alloc] initWithCustomView:_SHView dismissWhenTouchedBackground:YES];
    }
    return _alerView;
}

- (JCAlertView *)deleAlerView{
    if (_deleAlerView == nil){
        _deleImagesView = [[LCustomAlertView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight]) alertMessage:@"是否删除图片" titlesArray:@[@"取消",@"确认"]];
        _deleImagesView.delegate = self;
        _deleAlerView = [[JCAlertView alloc] initWithCustomView:_deleImagesView dismissWhenTouchedBackground:YES];
    }
    return _deleAlerView;
}
- (ZJIssueOrganizationSelectView *)organizationSelectView{
    if (_organizationSelectView == nil){
        _organizationSelectView = [[ZJIssueOrganizationSelectView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _organizationSelectView.delegate = self;
    }
    return _organizationSelectView;
}
@end
